package com.party.mobile.task;

import com.party.common.constant.Constant;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.DeployType;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.member.IMemberMerchantService;
import com.party.core.service.wechat.IWechatAccountService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.mobile.biz.wechat.WechatBizService;
import com.party.mobile.biz.wechat.WechatTokenBizService;
import com.party.mobile.web.dto.wechat.input.GetTicketResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 刷新微信方面数据
 * Created by wei.li
 *
 * @date 2016/12/16 0016
 * @time 15:54
 */

@Component(value = "quartzForWechat")
public class QuartzForWechat {

    @Autowired
    WechatTokenBizService wechatTokenBizService;

    @Autowired
    WechatBizService wechatBizService;

    @Autowired
    IWechatAccountService wechatAccountService;

    @Autowired
    IMemberMerchantService memberMerchantService;

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    private static final Logger logger = LoggerFactory.getLogger(QuartzForWechat.class);

    @Scheduled( fixedRate = 1000*60*90)
    public void refresh(){
        WechatOpenMember wechatOpenMember = new WechatOpenMember();
        wechatOpenMember.setDeployType(DeployType.FILL_IN.getCode());
        List<WechatOpenMember> list = wechatOpenMemberService.list(wechatOpenMember);
        for (WechatOpenMember w : list){
            try {
                if (DeployType.FILL_IN.getCode().equals(w.getDeployType())){
                    String token;
                    boolean refreshToken = true;
                    if (Constant.WECHAT_ACCOUNT_TYPE_SYSTEM.equals(w.getType())){
                        token = wechatTokenBizService.getToken();
                    }
                    else {
                        refreshToken = wechatBizService.refreshToken(w.getAuthorizerAppid());
                        if (!refreshToken) {
                            continue;
                        }
                        token = wechatTokenBizService.getToken(w.getAuthorizerAppid(), w.getSecret());
                    }
                    if (StringUtils.isEmpty(token)) {
                        continue;
                    }
                    w.setAuthorizerAccessToken(token);
                    w.setUpdateDate(new Date());
                    wechatOpenMemberService.update(w);

                    if (StringUtils.isNotEmpty(wechatOpenMember.getAuthorizerAppid()) && wechatOpenMember.getAuthorizerAppid().equals("wx1c8409c404069a43")) {
                        logger.info(getClass() + "   更新wechatOpenMember的{}token信息；refreshToken：{}", wechatOpenMember.getAuthorizerAppid(), refreshToken);
                    }
                }
                GetTicketResponse getTicketResponse = wechatBizService.getTicket(w.getAuthorizerAccessToken());
                if (null != getTicketResponse && null != getTicketResponse.getTicket()){
                    w.setTicket(getTicketResponse.getTicket());
                    w.setUpdateDate(new Date());
                    wechatOpenMemberService.update(w);
                }
            } catch (Exception e) {
                logger.error("刷新公众号信息异常——appId：" + w.getAuthorizerAppid() + "；appName:" + w.getNickName(), e);
                continue;
            }
        }
    }
}
